package db

import (
	"gorm.io/gorm/logger"
	"time"
)

const (
	Mysql = "mysql"
)

type Config struct {
	Driver          string       `yaml:"Driver"`
	DSN             string       `yaml:"DSN"`
	Logger          LoggerConfig `yaml:"Logger"`
	MaxIdleConns    int          `yaml:"MaxIdleConns"`
	MaxOpenConns    int          `yaml:"MaxOpenConns"`
	ConnMaxIdleTime int64        `yaml:"ConnMaxIdleTime"`
}

type LoggerConfig struct {
	SlowThreshold             string          `yaml:"SlowThreshold"`             //慢查询定义，格式：2s 1s 200ms
	Colorful                  bool            `yaml:"Colorful"`                  //
	IgnoreRecordNotFoundError bool            `yaml:"IgnoreRecordNotFoundError"` //忽略 NotFoundError
	ParameterizedQueries      bool            `yaml:"ParameterizedQueries"`      //隐藏查询参数
	LogLevel                  logger.LogLevel `yaml:"LogLevel"`                  //日志打印级别 1 Silent, 2 Error, 3 Warn, 4 Info
}

func (l LoggerConfig) Build() logger.Config {
	c := logger.Config{
		Colorful:                  l.Colorful,
		IgnoreRecordNotFoundError: l.IgnoreRecordNotFoundError,
		ParameterizedQueries:      l.ParameterizedQueries,
		LogLevel:                  l.LogLevel,
	}
	if v, err := time.ParseDuration(l.SlowThreshold); err == nil {
		c.SlowThreshold = v
	}
	return c
}
